//
//  JJImageCutController.swift
//  JJImagePicker
//
//  Created by 李加建 on 2021/5/31.
//

import UIKit

class ImageCutController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.black
        setup()
        setNavi()
    }

    var manager: ImagePickerManager

    init(manager: ImagePickerManager) {
        self.manager = manager
        super.init(nibName: nil, bundle: nil)
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        .lightContent
    }

    lazy var areaLayer: PhotoClipAreaLayer = {
        let width = view.bounds.width - 40
        let pointY = (view.bounds.height - width) / 2
        let rect = CGRect(x: 20, y: pointY, width: width, height: width)
        let lazy = PhotoClipAreaLayer(frame: view.bounds, centerRect: rect)
        return lazy
    }()

    lazy var photoView: PhotoClipScrollView = {
        let lazy = PhotoClipScrollView(frame: view.bounds)
        lazy.currentImage = self.manager.editImage
        return lazy
    }()

    lazy var cancelButton: UIButton = {
        let lazy = UIButton()
        lazy.setTitle("取消", for: .normal)
        lazy.addTarget(self, action: #selector(cancelButtonAction), for: .touchUpInside)
        return lazy
    }()

    lazy var doneButton: UIButton = {
        let lazy = UIButton()
        lazy.backgroundColor = UIColor(red: 255.0 / 255.0, green: 217.0 / 255.0, blue: 84.0 / 255.0, alpha: 1.0)
        lazy.clipsToBounds = true
        lazy.layer.cornerRadius = 16
        lazy.setTitle("确定", for: .normal)
        lazy.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 0.82), for: .normal)
        lazy.titleLabel?.font = UIFont(name: "PingFangSC-Medium", size: 15)
        lazy.addTarget(self, action: #selector(doneButtonAction), for: .touchUpInside)

        return lazy
    }()

    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        updateSafeArea()
    }
}

extension ImageCutController {
    func setup() {
        view.addSubview(photoView)
        view.layer.addSublayer(areaLayer)
        areaLayer.setNeedsDisplay()
        view.addSubview(cancelButton)
        view.addSubview(doneButton)
    }

    func setNavi() {
        let attributes: [NSAttributedString.Key: Any] = [
            NSAttributedString.Key.foregroundColor: UIColor.black,
            NSAttributedString.Key.font: UIFont(name: "PingFangSC-Semibold", size: 18) as Any,
        ]
        navigationController?.navigationBar.titleTextAttributes = attributes
        let image = UIImage.createImage(color: .clear, size: CGSize(width: 44, height: 44))
        navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.setBackgroundImage(image, for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.navigationBar.backgroundColor = .clear

        let backItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
        backItem.tintColor = .white
        navigationItem.leftBarButtonItem = backItem
    }

    func updateSafeArea() {
        let pointY = view.frame.height - view.safeAreaInsets.bottom - 52 - 20
        doneButton.frame = CGRect(x: view.frame.width - 148, y: pointY, width: 128, height: 52)
        cancelButton.frame = CGRect(x: 20, y: pointY, width: 60, height: 52)
    }

    @objc
    func cancelButtonAction() {
        navigationController?.popViewController(animated: true)
    }

    @objc
    func doneButtonAction() {
        if photoView.canclip {
            guard let image = photoView.clipperImage() else { return }
            manager.delegate?.didSeletedImage(image)
            navigationController?.dismiss(animated: true, completion: nil)
        }
    }
}
